package com.zx.ruiji.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zx.ruiji.common.R;
import com.zx.ruiji.entity.Employee;
import com.zx.ruiji.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
//        md5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

//        查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);

//        查询结果处理
        if (emp == null || !emp.getPassword().equals(password)) {
            return R.error("登录失败");
        } else if (emp.getStatus() == 0) {
            return R.error("该账号被禁用");
        } else {
            request.getSession().setAttribute("employee", emp.getId());
            return R.success(emp);
        }
    }


    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
//        清除session数据
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }


    //新增员工
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee) {
        log.info("新增员工信息：{}",employee.toString());
//        md5加密--设置初始密码;设置员工信息
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());

//        获取id创建用户
//        Long empId = (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);
        employeeService.save(employee);
        return R.success("新增成功");
    }


    //分页查询
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
//        分页构造器
        Page pageInfo = new Page(page, pageSize);
//        条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
//        分页页面展示形式
        queryWrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        queryWrapper.orderByDesc(Employee::getUpdateTime);
//        分页查询
        employeeService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }


    //账号启用、禁用,根据id修改员工信息
    @PutMapping
    public R<String> update(HttpServletRequest request, @RequestBody Employee employee) {
//        long id = Thread.currentThread().getId();
//        log.info("线程id为:{}", id);
//        Long empId = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(empId);
        employeeService.updateById(employee);
        return R.success("修改信息成功");
    }


    //根据id查询员工信息--编辑时信息回显
    @GetMapping("/{id}")
    public R<Employee> getById(HttpServletRequest request, @PathVariable Long id) {
        log.info("根据id查询员工信息");
        Employee employee = employeeService.getById(id);
        if(employee!=null) {
            return R.success(employee);
        }
        return R.error("没有查询到用户信息");
    }


}

